<template>
  <div class="memberCenter_box">
    <tab :tabs="tablist" activeName="personal"></tab>
    <!-- 用户信息 -->
    <section class="memberCenter_wrapper">
      <router-link tag="div" :to="{name: 'personalInfo'}" class="member_left left pointer">
        <div class="avatar_box">
          <img :src="uploadImg" width="130" height="130"/>
        </div>
        <div class="person_box">
          <div class="person_info">
            <span class="name">{{username}}</span>
            <span class="examine-result" @click.stop.prevent="$store.state.examine.examresultbox = !examresult_status">{{examTypes[exam]}}</span>
          </div>
          <div class="person_amount">
            <router-link :to="{name: 'MemberProcurement'}" class="title font-blue">当前采购额￥{{levelInfo.amount | floatPrices}}</router-link>
          </div>
          <div class="bar_progress">
            <span :style="{width: barwidth}" class="bar_fill"></span>
          </div>
          <div class="level ellipsis">
            等级V{{levelInfo.level ? levelInfo.level : 'IP'}}
            <span class="right font991220 ellipsis" v-if="!levelInfo.fixedLevel" style="width: 180px; line-height: 30px;">
              <em v-show="levelInfo.relegationAmount > 0 ">差{{levelInfo.relegationAmount}}元可保级 </em>
              &nbsp; &nbsp;
              <em v-show="nextLevelAmount">差{{nextLevelAmount}}元可升级</em>
            </span>
          </div>
        </div>
      </router-link>
      <div class="member_right right">
        <ul>
          <li class="member_right_item">
            <span class="member_right_title">待用优惠券</span> 
            <span class="member_right_content">{{discountCount}}张</span>
            </li>
          <li class="member_right_item">
            <span class="member_right_title">待领礼包</span>
            <span class="member_right_content">{{giftCount}}张</span>
            </li>
          <li class="member_right_item">
            <span class="member_right_title">可用承兑</span>
            <span class="member_right_content">¥{{acceptanceAmount | floatPrices}} <router-link :to="{name: 'MemberAcceptance'}" class="color-blue">(明细)</router-link></span>
          </li>
          <li class="member_right_item">
            <span class="member_right_title">可用余额</span>
            <span class="member_right_content">¥{{balanceAmount | floatPrices}} <router-link :to="{name: 'MemberBalance'}" class="color-blue">(明细)</router-link></span>
          </li>
          <li class="member_right_item">
            <span class="member_right_title">我的返利</span>
            <span class="member_right_content">¥0</span>
          </li>
          <li class="member_right_item">
            <span class="member_right_title">我的积分</span>
            <span class="member_right_content">0</span>
          </li>
        </ul>
      </div>
    </section>
    <div class="member_money">
    </div>
    <div class="line" ></div>
    <div class="holdPrivilege" >
      <p class="overhidden">
        我的专享特权
        <router-link :to="{name: 'memberPrivilege'}" class="font991220 right pointer">更多专享特权></router-link>
      </p>
      <privilege :levelInfoObj="levelInfo" :all="true"/>
    </div>
    <div class="line"></div>
    <div class="ad_box_block">
      <div class="qrcode">
        <img src="../../../images/qrcode.jpg" height="126" width="126" />
      </div>
      <h2 class="text_big">扫码关注微信公众号</h2>
      <p class="text_small">百万优惠 无限礼包 随意拿</p>
    </div>
  </div>
</template>
<script>
  import {getPreferList, getGiftCenter} from '@/service/get-data'
  import {phoneReplace} from '@/tool/tool'
  import {mapState, mapActions} from 'vuex'
  import {prefixImg, examTypes} from '@/tool/index'
  import {privilege, tab} from '@/components/index'
  import {getCookie} from '@/config/mUntils'
  export default{
    components: {
      privilege,
      tab
    },
    data () {
      return {
        tablist: [{name: 'personal', text: '个人信息'}],
        defaultUrl: require('../../../images/person_logo.png'),
        deductionList: [], // 优惠券
        giftList: [] // 礼包数量
      }
    },
    // beforeRouteEnter (to, from, next) {
    //   console.log(`${getCookie('Exam')}`)
    //   // ...
    //   if (this.Token) {
    //     if (this.Exam === 3) {
    //       this.$store.commit('EXAMNODO', true)
    //       next()
    //     } else if (this.Exam === 2) {
    //       this.$store.commit('EXAMFAIL', true)
    //       next()
    //       return false
    //     } else {
    //       this.$message.error('您的企业信息还在审核中,商品不能加入购物车,我们尽快给您审核.')
    //       next()
    //       return false
    //     }
    //   }
    // },
    computed: {
      ...mapState({
        username: state => state.user.username,
        exam: state => state.user.isExam,
        headImg: state => state.user.headImg,
        levelInfo: state => state.levelInfoObj,
        acceptanceAmount: state => state.user.acceptanceAmount,
        balanceAmount: state => state.user.balanceAmount,
        examresult_status: state => state.examine.examresultbox
      }),
      nextLevelAmount () {
        let result = this.levelInfo.amount >= this.levelInfo.nextLevelAmount ? 0 : (this.levelInfo.nextLevelAmount - this.levelInfo.amount).toFixed(2)
        return result
      },
      barwidth () {
        // 固定等级直接返回0%
        if (this.levelInfo.fixedLevel) {
          return '0%'
        }
        if (this.levelInfo.level === 5 || this.levelInfo.level === 0) {
          return '100%'
        }
        let calc = this.levelInfo.amount / this.levelInfo.nextLevelAmount
        calc = Number(calc * 100).toFixed(1)
        return calc + '%'
      },
      examTypes () {
        return examTypes
      },
      uploadImg () {
        return this.headImg ? prefixImg + this.headImg : this.defaultUrl
      },
      discountCount () {
        return this.deductionList.length
      },
      giftCount () {
        return this.giftList.length
      },
      Token () {
        return getCookie('Token')
      },
      Exam () {
        return parseInt(getCookie('Exam'))
      }
    },

    methods: {
      ...mapActions(['getUserLevelInfo']),
      // 获取优惠列表
      async fetchPreferList () {
        let res = await getPreferList(1, 10000, 0, undefined, false)
        if (res.data.resultCode && res.data.resultCode === 2000) {
          res = res.data
          this.deductionList = res.data.list
        } else {
          this.$message({message: res.data.resultMsg})
        }
      },
      // 礼包列表
      async fetchGiftCenter () {
        let res = await getGiftCenter(1, 1000, 0)
        if (res.data.resultCode && res.data.resultCode === 2000) {
          res = res.data
          this.giftList = res.data.list
        } else {
          this.$message.error(res.data.resultMsg)
        }
      },
      // 用户审核状态
      userState () {
        return new Promise((resolve, reject) => {
          if (this.Exam === 1) {
            resolve()
          } else if (this.Exam === 3) {
            this.$router.push({name: 'Index'})
            this.$store.commit('EXAMNODO', true)
            reject(new Error('未审核'))
          } else if (this.Exam === 2) {
            this.$router.push({name: 'Index'})
            this.$store.commit('EXAMFAIL', true)
            reject(new Error('审核失败'))
            return false
          } else {
            this.$router.push({name: 'Index'})
            this.$message.error('您的企业信息还在审核中,个人中心还不能查看,我们尽快给您审核.')
            reject(new Error('审核中'))
            return false
          }
        })
      }
    },
    mounted () {
      this.userState().then(() => {
        this.getUserLevelInfo()
        this.fetchPreferList()
        this.fetchGiftCenter()
      }).catch((err) => {
        console.log(err)
      })
    },
    filters: {
      phoneReplace
    }
  }
</script>
<style lang="scss" scoped>
  .memberCenter_wrapper{
    height: 200px;
    padding: 20px 0;
    margin-top: -20px;
    overflow: hidden;
  }
  .avatar_box{
    position: relative;
    display: inline-block;
    width: 115px;
    height: 115px;
    // vertical-align: middle;
    // border: 1px solid #DCDCDC;
    // padding: 2px;
    img{
      width: 109px;
      height: 109px;
      position: absolute;
      border: 1px solid #DCDCDC;
      border-radius: 50%;
      padding: 3px;
    }
  }
  .person_box{
      vertical-align: top;
      width: 60%;
      line-height: 30px;
      padding-left: 15px;
      display: inline-block;
      .name{
        font-size: 20px;
        color: #333333;
      }
      .examine-result{
        display: inline-block;
        border: 1px solid #FFCC00;
        border-radius: 4px;
        font-size: 14px;
        color: #FFCC00;
        height: 32px;
        line-height: 32px;
        text-align: center;
        width: 88px;
        margin-left: 10px;
      }
      // .level{
      //   line-height: 1.5;
      //   display: inline-block;
      //   border: 1px solid #FFCC00;
      //   border-radius: 2px;
      //   padding: 0 6px;
      // }
      .level{
        margin-top: 10px;
      }
      .person_amount{
        margin-top: 15px;
      }
      .title{
        @include fontbase(#036BEC, 14px, 32px);
      }
    }
    .member_left{
      width: 50%;
      height: 100%;
      padding: 24px 0 0 0;
      border-right: 1px dashed #dcdcdc;
      box-sizing: border-box;
    }
    .member_right{
      width: 50%;
      height: 100%;
      color: #666;
      padding: 24px 0 0 40px;
      box-sizing: border-box;
      &_item{
        width: 100%;
        margin-bottom: 5px;
        float: left;
        line-height: 20px;
      }
      &_title{
        font-size: 14px;
        margin-right: 8px;
        color: #333;
        width: 80px;
        display: inline-block;
      }
      &_content{
        color: #036BEC;
        float: right;
      }
    }
    .member_money{
      overflow: hidden;
      font-size: 13px;
      padding-top: 10px;
      color: #333;
      overflow: hidden;
      margin-bottom: 20px;
      
      .level{
        margin-top: 10px;
      }
    }
    .bar_progress{
      position: relative;
      height: 10px;
      border-radius: 5px;
      background: #f4f4f4;
      overflow: hidden;
      .bar_fill{
          width: 0;
          height: 10px;
          position: absolute;
          top: 0;
          left: 0;
          bottom: 0;
          background: #ffcc00;
          border-radius: 6px;
      }
    }
    .holdPrivilege{
      padding-top: 40px;
      margin-bottom: 40px;
    }
    .font991220{
      @include fontbase(#999999, 12px, 20px);
    }
</style>

